using Pb;
using Zdotp.Actor;
using Zdotp.Actor.Components;
using Zdotp.Actor.Controller;
using Zdotp.Actor.Kcp;
using Zdotp.Actor.Packet;
using Zdotp.Net.NetMessageHandler;

namespace DemoServer;

// [Component(typeof(TestComponent))]
public class TestActor : ActorBase
{
    protected override void OnAwake()
    {
        
    }

    protected override void OnStart()
    {
        
    }

    protected override void OnUpdate(float deltaTime)
    {
        
    }

    protected override void OnDestroy()
    {
        
    }
}

public class TestComponent : ComponentBase
{
    protected override void OnAdd()
    {
        Actor.Logger.Info("TestComponent OnAdd");
    }

    public override void OnUpdate(float deltaTime)
    {
        
    }

    public override void OnRemove()
    {
        Actor.Logger.Info("TestComponent OnRemove");
    }

    public void Print()
    {
        Actor.Logger.Info("TestComponent Print");
    }
}

public class TestMessage : MailObject
{
    public string Content;
}

public class TestActorHandler : ActorController<TestActor, TestMessage>
{
    protected override void Run(TestActor actor, ActorSystem system, MessageInfo info, TestMessage data)
    {
        var str = $"TestActor receive : {data.Content}";
        actor.Logger.Info(str);

        var component = actor.GetOrAddComponent<TestComponent>();
        component.Print();

        actor.SendMessage<WriteMessage>(message =>
        {
            message.Data = ActorSystem.GetNetPacket(new DataPacket { Content = str });
        }, 288, info.SessionId);
    }
}

public class ConnectController : ActorController<ActorKcp, ConnectMessage>
{
    protected override void Run(ActorKcp actor, ActorSystem system, MessageInfo info, ConnectMessage data)
    {
        actor.Logger.Info($"session {info.SessionId} connect...");
    }
}
public class ConnectHandler : ActorController<ActorKcp, DisConnectMessage>
{
    protected override void Run(ActorKcp actor, ActorSystem system, MessageInfo info, DisConnectMessage data)
    {
        actor.Logger.Info($"session {info.SessionId} disconnect...");
    }
}

public class DataPacketHandler : KcpMessageHandler<DataPacket>
{
    protected override void Run(ActorKcp kcpActor, ActorSystem system, uint sessionId, DataPacket netMessage)
    {
        var str = $"KcpActor receive : {netMessage.Content}";
        kcpActor.Logger.Info(str);
        
        kcpActor.SendMessage<TestMessage>(message =>
        {
            message.Content = netMessage.Content;
        }, 288, sessionId);
        
        // gate类内部使用SendMsgToClient回显给客户端
        kcpActor.SendMsgToClient(sessionId, new DataPacket {Content = str});
    }
}